﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MinhTam.DataAccess.Web;
using MinhTam.Objects;
using System.Data;
public partial class QuanLyTuyenDuong: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {          
          
            InitListData();
            txtMoTa.config.toolbar = HelpUtil.GetSimpleCKEditorConfig(this);
            txtMoTa.config.uiColor = HelpUtil.GetCKEditorUiColorAdmin(this);
        }
    }
    
    private void InitListData()
    {
        string sql = @"SELECT ROW_NUMBER() OVER (ORDER BY TD.BEN_DI,TD.BEN_DEN) STT,TD.TUYEN_DUONG_ID, TD.BEN_DI, TD.BEN_DEN,TD.MO_TA
FROM TUYEN_DUONG TD ";
        DataTable dt = SQLConnectWeb.GetTable(sql, TUYEN_DUONG.sTableName);
        gridViewData.DataKeyNames = new string[] {
            TUYEN_DUONG.cl_TUYEN_DUONG_ID, 
            TUYEN_DUONG.cl_BEN_DI,
            TUYEN_DUONG.cl_BEN_DEN, 
            TUYEN_DUONG.cl_MO_TA};
        gridViewData.AutoGenerateColumns = false;
        gridViewData.DataSource = dt;
        gridViewData.DataBind();

    }
    protected void gridViewData_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DataKey row = gridViewData.DataKeys[int.Parse(e.CommandArgument.ToString())];
        if (e.CommandName == "cmdEdit")
        {
         //   GridViewRow row = gridViewData.Rows[];
          
            keyField.Value = row.Values[TUYEN_DUONG.cl_TUYEN_DUONG_ID].ToString();
            txtBenDi.Text = row.Values[TUYEN_DUONG.cl_BEN_DI].ToString();
            txtBenDen.Text = row.Values[TUYEN_DUONG.cl_BEN_DEN].ToString();
        
            txtMoTa.Text = row.Values[TUYEN_DUONG.cl_MO_TA].ToString();
          
            btnSave.Text = "Cập nhật";

        }
        else if (e.CommandName == "cmdDelete")
        {
            string delete = row.Values[TUYEN_DUONG.cl_TUYEN_DUONG_ID].ToString();
           // HelpControls.ShowMsgBoxConfirmDelete(this);
            string sql = string.Format("delete from {0} where {1}={2}", TUYEN_DUONG.sTableName, TUYEN_DUONG.cl_TUYEN_DUONG_ID, delete);
            if (SQLConnectWeb.ExecSQL(sql))
            {
              //  HelpControls.ShowMsgBox(this, "Xóa dữ liệu thành công!");
                InitListData();
                if (delete == keyField.Value)
                    btnClearNew_Click(null, null);
            }
            else
            {
                HelpControls.ShowMsgBox(this, "Dữ liệu đang được sử dụng!");
            }
         
        }
    }

    protected void btnClearNew_Click(object sender, EventArgs e)
    {
        txtBenDen.Text = "";
       
        txtBenDi.Text = "";
        keyField.Value = "";
        txtMoTa.Text = "";
      
        btnSave.Text = "Lưu";
      
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(keyField.Value))//insert
        {
            DataTable dt = TUYEN_DUONG.GetTableFields(
                string.Format("( ({0}=N'{1}' and {2}=N'{3}') or ({0}=N'{3}' and {2}=N'{1}'))",
                TUYEN_DUONG.cl_BEN_DI,txtBenDi.Text.Trim(),TUYEN_DUONG.cl_BEN_DEN,txtBenDen.Text.Trim()),
                
                new string[] { }, TUYEN_DUONG.cl_TUYEN_DUONG_ID);
            if (dt != null && dt.Rows.Count > 0)
            {
                HelpControls.ShowMsgBox(this, "Đã tồn tại tuyến đường " + txtBenDi.Text + " <=> " + txtBenDen.Text + " (bao gồm cả chiều ngược lại)."); return;
            }

            TUYEN_DUONG obj = TUYEN_DUONG.Insert_Object(txtBenDi.Text.Trim(), txtBenDen.Text.Trim(), txtMoTa.Text.Replace("'", ""));
            if (obj == null)
                HelpControls.ShowMsgBox(this, "Lưu dữ liệu thất bại!");
            else
            {
                InitListData();
                keyField.Value = obj.TUYEN_DUONG_ID;
                btnSave.Text = "Cập nhật";
            }
            
        }
        else
        {
            DataTable dt = TUYEN_DUONG.GetTableFields(
               string.Format("( ({0}=N'{1}' and {2}=N'{3}') or ({0}=N'{3}' and {2}=N'{1}')) and {4} <> {5}",
               TUYEN_DUONG.cl_BEN_DI, txtBenDi.Text.Trim(), TUYEN_DUONG.cl_BEN_DEN, txtBenDen.Text.Trim(), TUYEN_DUONG.cl_TUYEN_DUONG_ID, keyField.Value),

               new string[] { }, TUYEN_DUONG.cl_TUYEN_DUONG_ID);
            if (dt != null && dt.Rows.Count > 0)
            {
                HelpControls.ShowMsgBox(this, "Đã tồn tại tuyến đường " + txtBenDi.Text + " <=> " + txtBenDen.Text + "  (bao gồm cả chiều ngược lại)."); return;
            }
            TUYEN_DUONG vp = new TUYEN_DUONG();
            vp.TUYEN_DUONG_ID = keyField.Value;
            if (vp.Save_Object(txtBenDi.Text.Trim(), txtBenDen.Text.Trim(), txtMoTa.Text.Replace("'","")))
            {
                InitListData();
            }
            else
            {
                HelpControls.ShowMsgBox(this, "Lưu dữ liệu thất bại!");
            }
        }
    }

    protected void gridViewData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)e.Row.Cells[1].Controls[0]).OnClientClick = "return confirm('Bạn có chắc là muốn xóa dữ liệu này?');";
        }
    }
}
